﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.BOL;
using System.Data;

namespace LDSP.UI.Admin
{
    public partial class AddEnterprise : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.CheckQS();
            }
            if (this.RB())
            {
                if (!IsPostBack)
                {
                    this.InitDataDynamicBefore();
                    this.InitDataSelect();
                }
            }
        }

        private void InitDataDynamicBefore()
        {
            g_pageInfo.PageSize = 10;
        }

        protected Enterprise m_enterprise = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_enterprise = null;
            }
        }
        protected Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (LastID != 0)
                    {
                        m_enterprise.GetProperties(LastID);
                    }
                }
                return m_enterprise;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (Enterprise.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void SetStatus()
        {
            //对于update页面，如果是打开的集团用户，则展示子公司列表
            this.tr_subdiary.Visible = Enterprise.IsEnterpriseGroup;

            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_code.Text = Enterprise.Code;
            this.txt_address.Text = Enterprise.Address;
            this.txt_contact.Text = Enterprise.Contact;
            this.txt_phone.Text = Enterprise.Phone;
            this.txt_comment.Text = Enterprise.Comment;
            this.txt_email.Text = Enterprise.Email;
            this.chb_isValid.Checked = Enterprise.IsValid;
            this.chk_isGroup.Checked = Enterprise.IsEnterpriseGroup;
            this.chb_supportManageFavorite.Checked = Enterprise.SupportManageFavorite;
            this.chb_supportSMS.Checked = Enterprise.SupportSMS;
            this.BindDDLIndustry();
            this.BindDDLParentCompany();
            this.BindGrid();

            this.SetStatus();
        }

        private void BindGrid()
        {
            string enterpriseId = Enterprise.ID.ToString();
            if (enterpriseId == "0")
            {
                enterpriseId = "-1";
            }

            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = new Enterprise().GetRecords("parentID=" + enterpriseId);
            this.grd_list.DataBind();
        }

        private void BindDDLParentCompany()
        {
            DataTable dt = new Enterprise().GetAll();
            this.ddl_ParentCompany.Items.Clear();
            this.ddl_ParentCompany.Items.Add(new ListItem("--请选择母公司--", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Enterprise en = new Enterprise();
                en.GetProperties(dt.Rows[i]);

                if (en.IsEnterpriseGroup)
                {
                    this.ddl_ParentCompany.Items.Add(new ListItem(dt.Rows[i]["code"].ToString(), dt.Rows[i]["id"].ToString()));
                }
            }
            try
            {
                this.ddl_ParentCompany.SelectedIndex = 0;
                this.ddl_ParentCompany.SelectedValue = Enterprise.ParentID.ToString();
            }
            catch { }
        }
        protected void BindDDLIndustry()
        {
            DataTable dt = new Industry().GetAll();
            this.ddl_industry.Items.Clear();
            this.ddl_industry.Items.Add(new ListItem("--请选择行业--", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_industry.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            try
            {
                this.ddl_industry.SelectedIndex = 0;
                this.ddl_industry.SelectedValue = Enterprise.IndustryID.ToString();
            }
            catch { }
        }

        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (!this.IsName(Enterprise.Code))
            {
                throw new CustomException("公司名只能包含汉子，字母，数字，-和下划线！");
            }
            if (!Enterprise.CanUpdate(null))
            {
                throw new CustomException("公司名已存在，请更换公司名！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            Enterprise.Code = this.txt_code.Text.Trim();
            Enterprise.Comment = this.txt_comment.Text;
            Enterprise.Address = this.txt_address.Text.Trim();
            Enterprise.Contact = this.txt_contact.Text.Trim();
            Enterprise.Phone = this.txt_phone.Text.Trim();
            Enterprise.IndustryID = this.GetInt(this.ddl_industry.SelectedValue);
            Enterprise.Email = this.txt_email.Text.Trim();
            Enterprise.IsValid = this.chb_isValid.Checked;
            Enterprise.IsEnterpriseGroup = this.chk_isGroup.Checked;
            Enterprise.SupportManageFavorite = this.chb_supportManageFavorite.Checked;
            Enterprise.SupportSMS = this.chb_supportSMS.Checked;
            Enterprise.ParentID = this.GetInt(this.ddl_ParentCompany.SelectedValue);
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            Enterprise.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            string tmp = Enterprise.Code;
            this.SetData();
            if (tmp != Enterprise.Code)
            {
                new DownloadLog().UpdateByEnterprise(Enterprise.ID, Enterprise.Code);
            }
            Enterprise.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Enterprise ett = new Enterprise();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);

                    this.ShowSequenceNumber(e.Row, 0);
                    e.Row.Cells[1].Text = ett.Industry.Name;
                    e.Row.Cells[7].Text = ett.IsValid ? "是" : "否";
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}